<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改题库管理')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-pool-edit" th:object="${pool}">
            <h4 class="form-header h4">题库管理信息</h4>
            <input name="poolId" th:field="*{poolId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">题库名称：</label>
                <div class="col-sm-8">
                    <input name="poolName" th:field="*{poolName}" class="form-control" type="text" required>
                </div>
            </div>
            <h4 class="form-header h4">题目管理信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/pool";
        var categoryDatas = [[${@dict.getType('topic_category')}]];
        var removeFlag = [[${@permission.hasPermi('system:topic:remove')}]];
        var lastDoneStateDatas = [[${@dict.getType('topic_last_done_state')}]];
        $("#form-pool-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-pool-edit').serialize());
            }
        }

        $(function() {
            var options = {
                removeUrl: ctx + "system/topic/remove",
                data: [[${pool.topicList}]],
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                modalName: "题目",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
                {
                    field: 'topicId',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input type='hidden' class='form-control' name='topicList[%s].topicId' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'question',
                    align: 'center',
                    title: '题目',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicList[%s].question' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'answer',
                    align: 'center',
                    title: '答案',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' type='text' name='topicList[%s].answer' value='%s'>", index, value);
                        return html;
                    }
                },

                {
                    field: 'category',
                    align: 'center',
                    title: '题目类型',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("topicList[%s].category", index);
                        return $.common.dictToSelect(categoryDatas, value, name);
                    }
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                        var id = row.topicId
                        return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="delRowByIndex(\'' + value + '\', \'' + id + '\')"><i class="fa fa-remove"></i>删除</a>';
                    }
                }]
            };
            $.table.init(options);
        });
        function delRowByIndex(value, id) {
            if (id == ''){
                sub.editRow();
                table.set();
                $("#" + table.options.id).bootstrapTable('remove', { field: "index", values: [value] });
                sub.editRow();
            }else {
                $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗？", function() {
                    var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
                    if (table.options.type == table_type.bootstrapTreeTable) {
                        $.operate.get(url);
                    } else {
                        var data = { "ids": id };
                        $.operate.submit(url, "post", "json", data);
                    }
                    sub.editRow();
                    table.set();
                    $("#" + table.options.id).bootstrapTable('remove', { field: "index", values: [value] });
                    sub.editRow();
                });
            }
        };

        function delRow(column) {
            debugger
            sub.editRow();
            var subColumn = $.common.isEmpty(column) ? "topicId" : column;
            var ids = $.table.selectColumns(subColumn);
            if (ids.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            $.modal.confirm("确认要删除选中的" + ids.length + "条数据吗?", function () {
                debugger
                var url = table.options.removeUrl;
                var data = {"ids": ids.join()};
                $.operate.submit(url, "post", "json", data);
                $("#" + table.options.id).bootstrapTable('remove', {field: subColumn, values: ids});
            });
        };

        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                topicId: "",
                question: "",
                answer: "",
                category: "",
                lastDoneState: "",
                lastDoneTime: "",
                createBy: "",
                createTime: "",
                updateBy: "",
                updateTime: "",
            }
            sub.addRow(row);
        }
    </script>
</body>
</html>